app.controller('AddSafetyModalCtrl', ['$rootScope', '$scope', '$stateParams', '$state', 'httpLoad', 'CommonData',
    function ($rootScope, $scope, $stateParams, $state, httpLoad, CommonData) {
        $rootScope.link = '/statics/css/tpl-card.css';//引入页面样式
        $scope.environmentData = CommonData.environmentData;
        $scope.typeData = CommonData.safetyType;
        $scope.targetCategoryName = 'FIREWALL';
        sessionStorage.setItem('networktabLocation', '2');
        //初始化默认值
        $scope.itemData = {
            type: 'FIREWALL',
            environment: 'DEVELOP'
        };
        var url = '/device/safety/create';
        var initDetailCreate = function () {
            //获取数据中心列表数据
            httpLoad.loadData({
                url: '/dc/list',
                noParam: true,
                success: function (data) {
                    if (data.success) {
                        $scope.dataCenterData = data.data.rows;
                    }
                }
            });
            //var loadObj = [$scope.itemData.dcId, $scope.itemData.roomId];
            //for (var a in loadObj) {
            //	$scope.loadSelectData(a, loadObj[a], 1);
            //}
        };
        // 如果为编辑，进行赋值
        var initDetailEdit = function (id) {
            url = '/device/safety/modify';
            httpLoad.loadData({
                url: '/device/safety/detail',
                method: 'GET',
                data: {id: id},
                success: function (data) {
                    if (data.success) {
                        $scope.itemData = data.data;
                        if ($scope.itemData.serviceStart) {
                            $scope.date = $scope.itemData.serviceStart + ' - ' + $scope.itemData.serviceEnd;
                        }
                        $scope.nodeData = {
                            targetName: $scope.itemData.name,
                            targetCategory: data.data.type,
                            relations: $scope.itemData.resourceRelations
                        };
                        $scope.isDataLoad = true;
                        //$scope.spiderData = {
                        //    resourceRelations: $scope.itemData.resourceRelations,
                        //    id: id,
                        //    targetCategory: data.data.type,
                        //    target: null
                        //}
                        //$scope.isDataShow = true;
                        //加载选择数据
                        var loadObj = [$scope.itemData.dcId, $scope.itemData.roomId];
                        loadObj.forEach(function(item,index){
                            $scope.loadSelectData(index,item, 1);
                        });
                    }
                }
            });
        };
        $scope.relations = function (value) {
            httpLoad.loadData({
                url: '/res/rule/relations',
                data: {
                    category: value
                },
                success: function (data) {
                    if (data.success) {
                        $scope.typeDataone = data.data;
                    }
                }
            })
        };

        (function () {
            switch ($stateParams.flag / 1) {
                case 4:
                    $rootScope.moduleTitle = '资源管理 > 添加安全设备';
                    $scope.modalName = '添加安全设备';
                    $scope.sourceDisabled = false;
                    $scope.spiderData = {
                        resourceRelations: null,
                        id: null,
                        targetCategory: $scope.targetCategoryName,
                        target: null
                    };
                    initDetailCreate();
                    break;
                case 5:
                    $rootScope.moduleTitle = '资源管理 > 编辑安全设备';
                    $scope.modalName = '编辑安全设备';
                    initDetailCreate();
                    initDetailEdit($stateParams.id);
                    break;
            };
        })();
        //四级联动加载数据
        $scope.loadSelectData = function (level, value, flag) {//flag用来判断编辑还是选择，编辑时不清空id
            if(!value) return;
            var loadRoom = function () {
                var param = [{param: {dcId: value}, sign: 'EQ'}];
                httpLoad.loadData({
                    url: '/room/list',
                    noParam: true,
                    data: {
                        simple: true,
                        params: JSON.stringify(param)
                    },
                    success: function (data) {
                        if (data.success) {
                            if(!flag){
                                $scope.cabinetData = [];
                                $scope.itemData.roomId = '';
                                $scope.itemData.rackId = '';
                            };
                            $scope.roomData = data.data.rows;
                        }
                    }
                })
            };
            var loadRack = function () {
                var param = [{param: {roomId: value}, sign: 'EQ'}];
                httpLoad.loadData({
                    url: '/rack/list',
                    noParam: true,
                    data: {
                        simple: true,
                        params: JSON.stringify(param)
                    },
                    success: function (data) {
                        if (data.success) {
                            $scope.cabinetData = data.data.rows;
                            if(!flag){
                                $scope.itemData.rackId = '';
                            }
                        }
                    }
                })
            };
            switch (level / 1) {
                case 0:
                    loadRoom();
                    break;
                case 1:
                    loadRack();
                    break;
            }
        };
        $scope.showPassword = function (value) {
            $scope[value] = !$scope[value];
        }
        var toFormatTime = function (time, place) {
            if (!time) return "";
            var date = time.split(' - ');
            return date[place / 1];
        }
        //保存按钮
        $scope.save = function () {
            //$scope.itemData.resourceRelations = $scope.spider.Adddata();
            if ($scope.date) {
                $scope.itemData.serviceStart = toFormatTime($scope.date, 0);
                $scope.itemData.serviceEnd = toFormatTime($scope.date, 1);
            }
            httpLoad.loadData({
                url: url,
                data: $scope.itemData,
                success: function (data) {
                    if (data.success) {
                        $scope.pop($stateParams.id ? '安全设备编辑成功' : '安全设备添加成功');
                        $scope.goBack();
                    }
                }
            });
        }
        $scope.goBack = function () {
            history.go(-1);
        };
    }
]);